<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>scoped_enum</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="prev" href="../boost/unwrap_ref.html" title="Function template unwrap_ref">
<link rel="next" href="swap.html" title="swap">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost/unwrap_ref.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="swap.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="core.scoped_enum"></a><a class="link" href="scoped_enum.html" title="scoped_enum">scoped_enum</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="scoped_enum.html#core.scoped_enum.overview">Overview</a></span></dt>
<dt><span class="section"><a href="scoped_enum.html#core.scoped_enum.deprecated_syntax">Deprecated syntax</a></span></dt>
<dt><span class="section"><a href="scoped_enum.html#core.scoped_enum.acquiring_the_underlying_type_of">Acquiring
      the underlying type of the enum</a></span></dt>
<dt><span class="section"><a href="scoped_enum.html#core.scoped_enum.acknowledgments">Acknowledgments</a></span></dt>
</dl></div>
<div class="simplesect">
<div class="titlepage"><div><div><h3 class="title">
<a name="idm46136446553680"></a>Authors</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Beman Dawes
        </li>
<li class="listitem">
          Vicente J. Botet Escriba
        </li>
<li class="listitem">
          Anthony Williams
        </li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.scoped_enum.overview"></a><a class="link" href="scoped_enum.html#core.scoped_enum.overview" title="Overview">Overview</a>
</h3></div></div></div>
<p>
        The <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">core</span><span class="special">/</span><span class="identifier">scoped_enum</span><span class="special">.</span><span class="identifier">hpp</span></code> header
        contains a number of macros that can be used to generate C++11 scoped enums
        (7.2 [dcl.enum]) if the feature is supported by the compiler, otherwise emulate
        it with C++03 constructs. The <code class="computeroutput"><span class="identifier">BOOST_NO_CXX11_SCOPED_ENUMS</span></code>
        macro from Boost.Config is used to detect the feature support in the compiler.
      </p>
<p>
        Some of the enumerations defined in the standard library are scoped enums.
      </p>
<pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">future_errc</span>
<span class="special">{</span>
    <span class="identifier">broken_promise</span><span class="special">,</span>
    <span class="identifier">future_already_retrieved</span><span class="special">,</span>
    <span class="identifier">promise_already_satisfied</span><span class="special">,</span>
    <span class="identifier">no_state</span>
<span class="special">};</span>
</pre>
<p>
        The user can portably declare such enumeration as follows:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_SCOPED_ENUM_DECLARE_BEGIN</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">broken_promise</span><span class="special">,</span>
    <span class="identifier">future_already_retrieved</span><span class="special">,</span>
    <span class="identifier">promise_already_satisfied</span><span class="special">,</span>
    <span class="identifier">no_state</span>
<span class="special">}</span>
<span class="identifier">BOOST_SCOPED_ENUM_DECLARE_END</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">)</span>
</pre>
<p>
        These macros allows to use <code class="computeroutput"><span class="identifier">future_errc</span></code>
        in almost all the cases as an scoped enum.
      </p>
<pre class="programlisting"><span class="identifier">future_errc</span> <span class="identifier">ev</span> <span class="special">=</span> <span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span><span class="special">;</span>
</pre>
<p>
        It is possible to specify the underlying type of the enumeration:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="keyword">int</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">broken_promise</span><span class="special">,</span>
    <span class="identifier">future_already_retrieved</span><span class="special">,</span>
    <span class="identifier">promise_already_satisfied</span><span class="special">,</span>
    <span class="identifier">no_state</span>
<span class="special">}</span>
<span class="identifier">BOOST_SCOPED_ENUM_DECLARE_END</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">)</span>
</pre>
<p>
        The enumeration supports explicit conversion from the underlying type.
      </p>
<p>
        The enumeration can be forward declared:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_SCOPED_ENUM_FORWARD_DECLARE</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">);</span>
</pre>
<p>
        There are however some limitations. The emulated scoped enum is not a C++
        enum, so <code class="computeroutput"><span class="identifier">is_enum</span><span class="special">&lt;</span>
        <span class="identifier">future_errc</span> <span class="special">&gt;</span></code>
        will be <code class="computeroutput"><span class="identifier">false_type</span></code>.
      </p>
<p>
        The emulated scoped enum can not be used in switch nor in template arguments.
        For these cases the user needs to use some helpers. Instead of
      </p>
<pre class="programlisting"><span class="keyword">switch</span> <span class="special">(</span><span class="identifier">ev</span><span class="special">)</span>
<span class="special">{</span>
<span class="keyword">case</span> <span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span><span class="special">:</span>
    <span class="comment">// ...</span>
</pre>
<p>
        use
      </p>
<pre class="programlisting"><span class="keyword">switch</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">native_value</span><span class="special">(</span><span class="identifier">ev</span><span class="special">))</span>
<span class="special">{</span>
<span class="keyword">case</span> <span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span><span class="special">:</span>
    <span class="comment">// ...</span>
</pre>
<p>
        and instead of
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;&gt;</span>
<span class="keyword">struct</span> <span class="identifier">is_error_code_enum</span><span class="special">&lt;</span> <span class="identifier">future_errc</span> <span class="special">&gt;</span> <span class="special">:</span>
    <span class="keyword">public</span> <span class="identifier">true_type</span>
<span class="special">{</span>
<span class="special">};</span>
</pre>
<p>
        use
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;&gt;</span>
<span class="keyword">struct</span> <span class="identifier">is_error_code_enum</span><span class="special">&lt;</span> <span class="identifier">BOOST_SCOPED_ENUM_NATIVE</span><span class="special">(</span><span class="identifier">future_errc</span><span class="special">)</span> <span class="special">&gt;</span> <span class="special">:</span>
    <span class="keyword">public</span> <span class="identifier">true_type</span>
<span class="special">{</span>
<span class="special">};</span>
</pre>
<p>
        Explicit conversion to the underlying type should be performed with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">underlying_cast</span></code> instead of <code class="computeroutput"><span class="keyword">static_cast</span></code>:
      </p>
<pre class="programlisting"><span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">val</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">underlying_cast</span><span class="special">&lt;</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="special">&gt;(</span><span class="identifier">ev</span><span class="special">);</span>
</pre>
<p>
        In C++03, scoped enums behave differently in case of calling an overloaded
        function when one overload takes a scoped enum as a parameter, and the other
        takes a parameter of an integral type. Consider the following code:
      </p>
<pre class="programlisting"><span class="keyword">enum</span> <span class="identifier">enum_regular</span> <span class="special">{</span> <span class="identifier">REGULAR_A</span><span class="special">,</span> <span class="identifier">REGULAR_B</span> <span class="special">};</span>

<span class="identifier">BOOST_SCOPED_ENUM_DECLARE_BEGIN</span><span class="special">(</span><span class="identifier">enum_scoped</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span>
<span class="special">}</span>
<span class="identifier">BOOST_SCOPED_ENUM_DECLARE_END</span><span class="special">(</span><span class="identifier">enum_scoped</span><span class="special">)</span>

<span class="keyword">void</span> <span class="identifier">regular_or_int</span><span class="special">(</span><span class="identifier">enum_regular</span><span class="special">);</span> <span class="comment">// (1)</span>
<span class="keyword">void</span> <span class="identifier">regular_or_int</span><span class="special">(</span><span class="keyword">int</span><span class="special">);</span>          <span class="comment">// (2)</span>
<span class="keyword">void</span> <span class="identifier">scoped_or_int</span><span class="special">(</span><span class="identifier">enum_scoped</span><span class="special">);</span>   <span class="comment">// (3)</span>
<span class="keyword">void</span> <span class="identifier">scoped_or_int</span><span class="special">(</span><span class="keyword">int</span><span class="special">);</span>           <span class="comment">// (4)</span>

<span class="identifier">regular_or_int</span><span class="special">(</span><span class="identifier">REGULAR_A</span><span class="special">);</span>                  <span class="comment">// calls (1) in C++03 and C++11</span>
<span class="identifier">scoped_or_int</span><span class="special">(</span><span class="identifier">enum_scoped</span><span class="special">::</span><span class="identifier">a</span><span class="special">);</span>              <span class="comment">// calls (3) in C++11 but (4) in C++03!</span>
<span class="identifier">scoped_or_int</span><span class="special">(</span><span class="identifier">enum_scoped</span><span class="special">(</span><span class="identifier">enum_scoped</span><span class="special">::</span><span class="identifier">a</span><span class="special">));</span> <span class="comment">// calls (3) in C++03 and C++11</span>
</pre>
<p>
        Here is usage example:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN</span><span class="special">(</span><span class="identifier">algae</span><span class="special">,</span> <span class="keyword">char</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">green</span><span class="special">,</span>
    <span class="identifier">red</span><span class="special">,</span>
    <span class="identifier">cyan</span>
<span class="special">}</span>
<span class="identifier">BOOST_SCOPED_ENUM_DECLARE_END</span><span class="special">(</span><span class="identifier">algae</span><span class="special">)</span>
<span class="special">...</span>
<span class="identifier">algae</span> <span class="identifier">sample</span><span class="special">(</span> <span class="identifier">algae</span><span class="special">::</span><span class="identifier">red</span> <span class="special">);</span>
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span> <span class="identifier">algae</span> <span class="identifier">color</span> <span class="special">);</span>
<span class="special">...</span>
<span class="identifier">sample</span> <span class="special">=</span> <span class="identifier">algae</span><span class="special">::</span><span class="identifier">green</span><span class="special">;</span>
<span class="identifier">foo</span><span class="special">(</span> <span class="identifier">algae</span><span class="special">::</span><span class="identifier">cyan</span> <span class="special">);</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.scoped_enum.deprecated_syntax"></a><a class="link" href="scoped_enum.html#core.scoped_enum.deprecated_syntax" title="Deprecated syntax">Deprecated syntax</a>
</h3></div></div></div>
<p>
        In early versions of the header there were two ways to declare scoped enums,
        with different pros and cons to each. The other way used a different set
        of macros:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_SCOPED_ENUM_START</span><span class="special">(</span><span class="identifier">algae</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">green</span><span class="special">,</span>
    <span class="identifier">red</span><span class="special">,</span>
    <span class="identifier">cyan</span>
<span class="special">};</span>
<span class="identifier">BOOST_SCOPED_ENUM_END</span>
<span class="special">...</span>
<span class="identifier">BOOST_SCOPED_ENUM</span><span class="special">(</span><span class="identifier">algae</span><span class="special">)</span> <span class="identifier">sample</span><span class="special">(</span> <span class="identifier">algae</span><span class="special">::</span><span class="identifier">red</span> <span class="special">);</span>
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span> <span class="identifier">BOOST_SCOPED_ENUM</span><span class="special">(</span><span class="identifier">algae</span><span class="special">)</span> <span class="identifier">color</span> <span class="special">);</span>
<span class="special">...</span>
<span class="identifier">sample</span> <span class="special">=</span> <span class="identifier">algae</span><span class="special">::</span><span class="identifier">green</span><span class="special">;</span>
<span class="identifier">foo</span><span class="special">(</span> <span class="identifier">algae</span><span class="special">::</span><span class="identifier">cyan</span> <span class="special">);</span>
</pre>
<p>
        Here <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM_START</span></code>
        corresponds to <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM_DECLARE_BEGIN</span></code>,
        <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM_END</span></code> to
        <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM_DECLARE_END</span></code>
        and <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM</span></code> to
        <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM_NATIVE</span></code>.
        Note also the semicolon before <code class="computeroutput"><span class="identifier">BOOST_SCOPED_ENUM_END</span></code>.
      </p>
<p>
        In the current version these macros produce equivalent result to the ones
        described above and are considered deprecated.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.scoped_enum.acquiring_the_underlying_type_of"></a><a class="link" href="scoped_enum.html#core.scoped_enum.acquiring_the_underlying_type_of" title="Acquiring the underlying type of the enum">Acquiring
      the underlying type of the enum</a>
</h3></div></div></div>
<p>
        The header <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">core</span><span class="special">/</span><span class="identifier">underlying_type</span><span class="special">.</span><span class="identifier">hpp</span></code> defines
        the metafunction <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">underlying_type</span></code> which can be used to obtain
        the underlying type of the scoped enum. This metafunction has support for
        emulated scoped enums declared with macros in <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">core</span><span class="special">/</span><span class="identifier">scoped_enum</span><span class="special">.</span><span class="identifier">hpp</span></code>. When native scoped enums are supported
        by the compiler, this metafunction is equivalent to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">underlying_type</span></code>.
      </p>
<p>
        Unfortunately, there are configurations which implement scoped enums but
        not <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">underlying_type</span></code>. In this case <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">underlying_type</span></code> has to be specialized by
        user. The macro <code class="computeroutput"><span class="identifier">BOOST_NO_UNDERLYING_TYPE</span></code>
        is defined to indicate such cases.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.scoped_enum.acknowledgments"></a><a class="link" href="scoped_enum.html#core.scoped_enum.acknowledgments" title="Acknowledgments">Acknowledgments</a>
</h3></div></div></div>
<p>
        This scoped enum emulation was developed by Beman Dawes, Vicente J. Botet
        Escriba and Anthony Williams.
      </p>
<p>
        Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott,
        Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida, Matt Calabrese, Daniel
        James and Andrey Semashev.
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p>
        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
        Software License, Version 1.0</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost/unwrap_ref.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="swap.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
